<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Navigation;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Tree;

class NavigationController extends AdminController
{
    public function index(Content $content)
    {
        return $content->header('導航管理')
            ->body(function (Row $row) {
                $tree = new Tree(new Navigation);
                $tree->branch(function ($branch) {
/*                    $src = config('admin.upload.host') . '/' . $branch['logo'] ;
                    $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";*/
                    $url = url($branch['link']?:"/");
                    $link = "<a style='margin-left: 10px' href='$url' class='dd-nodrag'>{$branch['link']}</a>";
                    return "{$branch['name']} $link";
                });

                $tree->disableCreateButton();
                $row->column(12, $tree);
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    /*protected function grid()
    {
        return Grid::make(new Navigation(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent_id');
            $grid->column('name');
            $grid->column('link');
            $grid->column('ico');
            $grid->column('sort');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }*/

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Navigation(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('name');
            $show->field('link');
            $show->field('ico');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {


        return Form::make(new Navigation(), function (Form $form) {

            $form->select('parent_id')->options(\App\Models\Navigation::where('parent_id',0)->pluck('name','id')->prepend('頂級',0))->default(0);
            $form->text('name');
            $form->text('link');
            $form->image('ico')->autoUpload()->uniqueName();

        });
    }
}
